import { IToolTipItem } from "./IToolTipItem";
import { ToolTipPool } from "./ToolTipPool";
import { IToolTip } from "./IToolTip";
type int = number;
//class ToolTipsManager
    
    export  class ToolTipsManager /* flash.display.Sprite */
    {
        constructor(arg1: Object /* flash.display.DisplayObjectContainer */)
        {
            
            visible = false;
            var loc1;
            mouseChildren = loc1 = false;
            mouseEnabled = loc1;
            this.toolTipLayer = arg1;
            this._this = this;
            arg1.addChild(this);
            return;
        }

        public targetMouseHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc1=null;
            var loc2=arg1.type;
            switch (loc2) 
            {
                case flash.events.MouseEvent.MOUSE_OUT: case flash.events.MouseEvent.MOUSE_DOWN: case flash.events.MouseEvent.MOUSE_UP:
                {
                    ToolTipsManager._instance.hide();
                    break;
                }
                case flash.events.MouseEvent.MOUSE_MOVE:
                {
                    ToolTipsManager._instance.move(new Object /* flash.geom.Point */(arg1.stageX, arg1.stageY));
                    break;
                }
                case flash.events.MouseEvent.MOUSE_OVER:
                {
                    loc1 = arg1.currentTarget as Object /* flash.display.DisplayObject */;
                    if (ToolTipsManager._instance.area != loc1) 
                    {
                        ToolTipsManager._instance.hide();
                    }
                    if (loc1.alpha == 1) 
                    {
                        ToolTipsManager._instance.area = arg1.currentTarget as Object /* flash.display.DisplayObject */;
                        this.startDelayCount();
                    }
                    break;
                }
            }
            return;
        }

        public show(arg1: Object /* flash.display.DisplayObject */): void
        {
            var loc1=null;
            var loc2=null;
            var loc3=null;
            this.area.addEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.targetMouseHandler);
            if (arg1.accessibilityProperties) 
            {
                this.clearConetnt();
                loc1 = ToolTipsManager.toolClassMap[arg1.accessibilityProperties.description];
                if (!loc1) 
                {
                    return;
                }
                if (typeof loc1.areaItem === "itooltipitem") 
                {
                    loc2 = (loc1.areaItem as IToolTipItem).toolTipData;
                }
                else 
                {
                    loc2 = loc1.toolTipData;
                }
                if (loc2 != null) 
                {
                    this.toolTipContent = ToolTipPool.getObject(loc1.toolTipClass as Class);
                    (this.toolTipContent as Object /* flash.display.DisplayObject */).visible = true;
                    this.toolTipContent.data = loc2;
                    (loc3 = this.toolTipContent as Object /* flash.display.DisplayObjectContainer */).mouseChildren = false;
                    loc3.mouseEnabled = false;
                    addChild(loc3);
                }
            }
            return;
        }

        public hide(): void
        {
            this.clearDelayCount();
            this.clearConetnt();
            if (this.area) 
            {
                this.area.removeEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.targetMouseHandler);
                this.area.removeEventListener(flash.events.MouseEvent.MOUSE_OUT, this.targetMouseHandler);
                this.area.removeEventListener(flash.events.MouseEvent.MOUSE_MOVE, this.targetMouseHandler);
                this.area = null;
            }
            visible = false;
            return;
        }

        /* internal  */clearConetnt(): void
        {
            var loc1=null;
            if (this.toolTipContent) 
            {
                ToolTipPool.disposeObject(this.toolTipContent);
                loc1 = this.toolTipContent as Object /* flash.display.DisplayObject */;
                loc1.visible = false;
                if (loc1.hasOwnProperty("dispose")) 
                {
                    var loc2;
                    (loc2 = loc1)["dispose"]();
                }
                if (this.contains(loc1)) 
                {
                    this.removeChild(loc1);
                }
                this.toolTipContent = null;
            }
            return;
        }

        public move(arg1: Object /* flash.geom.Point */): void
        {
            var loc1=this.parent.globalToLocal(arg1);
            this.x = loc1.x + 15;
            if (this.x > this.toolTipLayer.stage.stageWidth - this.width - 6) 
            {
                this.x = loc1.x - this.width - 6;
            }
            var loc2=1;
            this.y = loc1.y + 6;
            var loc3;
            if (loc3 = this.toolTipContent as Object /* flash.display.DisplayObject */) 
            {
                if (loc3.height > this.toolTipLayer.stage.stageHeight) 
                {
                    this.y = 0;
                }
                else if (this.y + loc3.height > this.toolTipLayer.stage.stageHeight) 
                {
                    this.y = this.toolTipLayer.stage.stageHeight - loc3.height;
                }
            }
            if (!visible) 
            {
                visible = true;
            }
            return;
        }

        /* internal  */startDelayCount(): void
        {
            if (!this.delayTimer) 
            {
                this.delayTimer = new Object /* flash.utils.Timer */(500, 1);
                this.delayTimer.addEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.showAfterDelay);
            }
            this.delayTimer.reset();
            this.delayTimer.start();
            this.area.addEventListener(flash.events.MouseEvent.MOUSE_DOWN, this.targetMouseHandler);
            this.area.addEventListener(flash.events.MouseEvent.MOUSE_OUT, this.targetMouseHandler);
            this.area.addEventListener(flash.events.MouseEvent.MOUSE_UP, this.targetMouseHandler);
            return;
        }

        /* internal  */clearDelayCount(): void
        {
            if (this.delayTimer) 
            {
                this.delayTimer.stop();
            }
            return;
        }

        /* internal  */showAfterDelay(arg1: Object /* flash.events.TimerEvent */): void
        {
            var loc1=null;
            if (this.area) 
            {
                this.show(this.area);
                loc1 = new Object /* flash.geom.Point */(this.area.mouseX, this.area.mouseY);
                loc1 = this.area.localToGlobal(loc1);
                this.move(loc1);
            }
            return;
        }

        public /* override */ get width(): number
        {
            if (this.toolTipContent && this.toolTipContent is flash.display.DisplayObject) 
            {
                return (this.toolTipContent as Object /* flash.display.DisplayObject */).width;
            }
            return super.width;
        }

        public static get defaultRenderClass(): Class
        {
            return ToolTipsManager._defaultRenderClass;
        }

        public static set defaultRenderClass(arg1: Class)
        {
            ToolTipsManager._defaultRenderClass = arg1;
            return;
        }

        public static init(arg1: Object /* flash.display.DisplayObjectContainer */): void
        {
            if (ToolTipsManager._instance == null) 
            {
                ToolTipsManager._instance = new ToolTipsManager(arg1);
            }
            return;
        }

        public static register(arg1, arg2: Class=null, arg3: Object=null): void
        {
            var loc1=null;
            if (ToolTipsManager._instance == null) 
            {
                throw new Error("ToolTipsManager 未初始化");
            }
            var loc2;
            if ((loc2 = arg1["accessibilityProperties"]) != null) 
            {
                loc1 = loc2.description;
            }
            else 
            {
                var loc4;
                arg1["accessibilityProperties"] = loc4 = new Object /* flash.accessibility.AccessibilityProperties */();
                loc2 = loc4;
                ToolTipsManager.classId++;
                loc1 = "Item" + ToolTipsManager.classId;
                loc2.description = loc1;
            }
            var loc3;
            (loc3 = arg1 as Object /* flash.events.EventDispatcher */).addEventListener(flash.events.MouseEvent.MOUSE_OVER, ToolTipsManager._instance.targetMouseHandler);
            if (arg2 == null) 
            {
                if (ToolTipsManager._defaultRenderClass == null) 
                {
                    throw new Error("ToolTipManger的dfaultRenderClass未定义");
                }
                else 
                {
                    ToolTipsManager.toolClassMap[loc1] = {"toolTipClass": ToolTipsManager._defaultRenderClass, "areaItem": arg1, "toolTipData": arg3};
                }
            }
            else 
            {
                ToolTipsManager.toolClassMap[loc1] = {"toolTipClass": arg2, "areaItem": arg1, "toolTipData": arg3};
            }
            return;
        }

        public static unregister(arg1: Object /* flash.events.EventDispatcher */): void
        {
            var loc1=null;
            var loc2=null;
            if (!(ToolTipsManager._instance == null) && arg1) 
            {
                loc2 = arg1["accessibilityProperties"];
                if (loc2) 
                {
                    loc1 = loc2.description;
                }
                ToolTipsManager.toolClassMap[loc1] = null;
                delete ToolTipsManager.toolClassMap[loc1];
                arg1.removeEventListener(flash.events.MouseEvent.MOUSE_OVER, ToolTipsManager._instance.targetMouseHandler);
            }
            return;
        }

        
        {
            ToolTipsManager.toolClassMap = {};
            ToolTipsManager.classId = 0;
            ToolTipsManager._instance = null;
        }

        private /* var */area: Object /* flash.display.DisplayObject */;

        private /* var */toolTipContent: IToolTip;

        private /* var */toolTipLayer: Object /* flash.display.DisplayObjectContainer */;

        private /* var */delayTimer: Object /* flash.utils.Timer */;

        private /* var */_this: ToolTipsManager;

        private static /* var */toolClassMap: Object;

        private static /* var */classId: int=0;

        private static /* var */_instance: ToolTipsManager=null;

        private static /* var */_defaultRenderClass: Class;
    }
